package com.example.a14110100409.yuejiandemo.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.a14110100409.yuejiandemo.R;
import com.example.a14110100409.yuejiandemo.activity.LoginActivity;
import com.example.a14110100409.yuejiandemo.activity.UserCollectActivity;
import com.example.a14110100409.yuejiandemo.activity.UserEditActivity;
import com.example.a14110100409.yuejiandemo.activity.UserOrderActivity;
import com.example.a14110100409.yuejiandemo.activity.UserTieziActivity;
import com.example.a14110100409.yuejiandemo.javabean.User;
import com.example.a14110100409.yuejiandemo.javabean.UserInfo;
import com.example.a14110100409.yuejiandemo.model.UserModel;
import com.example.a14110100409.yuejiandemo.utils.RoundImageView;

import it.sephiroth.android.library.picasso.Picasso;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MyFragment extends Fragment {
    private RecyclerView recyclerView;
    private TextView My_name;
    private ImageView My_photo,My_tiezi,My_collect,My_order;
    private int userid;
    private User userinfo;
    private UserInfo userInfo;
    private Context mContext;
    private Button my_edit,user_exit;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.activity_my_fragment,container,false);
        /*recyclerView = (RecyclerView) view.findViewById(R.id.my_rv);
        recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));//设置布局*/
        My_name = (TextView) view.findViewById(R.id.My_name);
        My_photo = (RoundImageView) view.findViewById(R.id.My_photo);
        My_tiezi = (ImageView) view.findViewById(R.id.MY_1p);
        My_collect = (ImageView) view.findViewById(R.id.MY_2p);
        My_order = (ImageView) view.findViewById(R.id.MY_3p);
        userid = getActivity().getSharedPreferences("userInfo", Context.MODE_PRIVATE).getInt("user_id",0);
        Log.v("获取id++++++++++", String.valueOf(userid));
        user_exit = (Button) view.findViewById(R.id.user_exit);
        user_exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
                //Toast.makeText(mContext, "退出登录成功", Toast.LENGTH_SHORT).show();
            }
        });
        my_edit = (Button) view.findViewById(R.id.my_edit);
        my_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),UserEditActivity.class);
                startActivity(intent);
            }
        });
        My_tiezi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),UserTieziActivity.class);
                intent.putExtra("user_name",userinfo.getUser_name());
                intent.putExtra("user_photo", (String) userinfo.getPhoto());
                startActivity(intent);
            }
        });
        My_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),UserCollectActivity.class);
                startActivity(intent);
            }
        });
        My_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),UserOrderActivity.class);
                intent.putExtra("user_name",userinfo.getUser_name());
                intent.putExtra("user_photo", (String) userinfo.getPhoto());
                startActivity(intent);
            }
        });
        showData();
        return view;
    }
    private void showData(){
        UserModel userModel = new UserModel();
        Call<User> call = userModel.getUserinfo(userid);
        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                userinfo = response.body();
                My_name.setText(userinfo.getUser_name());
                String address="http://172.24.10.175/workout/Uploads/"+userinfo.getPhoto();
                if(!"".equals(userinfo.getPhoto())){
                    Picasso.with(mContext)
                            .load(address)
                            .placeholder(R.drawable.tx)
                            .noFade()
                            .into(My_photo);
                }
                else {

                }
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {

            }
        });
    }
}
